<?php
namespace app\index\controller;
use think\Request;
use think\Session;
use think\Db;

class Index 
{
    public function _initialize()
    {
        parent::_initialize();
    }

    public function showindex()
    {
      return view('index/index');
    }

    // 菜单
    public function index()
    {
      $data = Db::table('category') -> field('id, name') ->select();
      // var_dump($data);die;

      for ($i=0; $i < count($data); $i++) { 
        $list = Db::table('goods')
                  ->field('id, name, price, details')
                  ->where('goods_categroy_id',$data[$i]['id'])
                  ->select();
        // var_dump($list);
        if (!empty($list)) {
          for ($a=0; $a < count($list); $a++) { 
            $data[$i]['goods'][$a]['gid'] = $list[$a]['id'];
            $data[$i]['goods'][$a]['gname'] = $list[$a]['name'];
            $data[$i]['goods'][$a]['price'] = $list[$a]['price'];
            $data[$i]['goods'][$a]['details'] = $list[$a]['details'];
          }        
        }else{
          $data[$i]['goods'][0]['gid'] = 'NONE';
          $data[$i]['goods'][0]['gname'] = '暂无哟';
          $data[$i]['goods'][0]['price'] = 0;
          $data[$i]['goods'][0]['details'] = '暂无哟';
        }   
      }

      // var_dump($data);die;
      return view('goods/goodslist',[
        'list'=>$data
      ]);

      
    }


    public function showgoods($id)
    {
        // var_dump($id);die;
        // var_dump($_GET);die;
        if($id !== 'NONE')
        {
          $res = Db::table('goods')->where('id',$id)->find();
          // var_dump($res);die;
          $mess = Db::table('goods') ->select();
          return view('goods/details',[
            'data' => $res,
            'list' => $mess
          ]);

        }else{
          echo "<script> alert('此菜品还未上线哟!');history.go(-1); </script>";die;
        }

    }



    // public function luyou()
    // {
    //   $res = Db::table('user')->field('nickname,tel')->selete();
    //   // var_dump($res);die;
    // }


    // public function myerror($msg,$url='',$time = 1)
    // {   
    //     $str='<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
    //           <script src="https://cdn.bootcss.com/layer/3.1.0/layer.js"></script>
    //           <script>$(function(){layer.msg("'.$msg.'",function(){});});</script>';
    //     echo $str;

    //     // $url 是空值, 默认跳转到 上一级
    //     if( empty($url) ){
    //         $url = $_SERVER['HTTP_REFERER'];
    //     }

    //     echo '<meta http-equiv="refresh" content="'.$time.'; url= '.$url.' ">';
    //     die;
    // }
   
}

